package sk.fritz.megafonsms;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemStateListener;
import javax.microedition.lcdui.TextField;

import sk.fritz.megafonsms.formmodel.AbstractFormControl;
import sk.fritz.megafonsms.formmodel.FormModel;
import sk.fritz.megafonsms.formmodel.InputFormControl;
import sk.fritz.megafonsms.formmodel.SelectFormControl;

public class UIFormBuilder {
	
	private FormModel model;
	
	public FormModel getModel() {
		return model;
	}


	public void setModel(FormModel model) {
		this.model = model;
	}


	private Vector modifiers = new Vector();
	
	public void addModifier(ItemModifier modifier) {
		modifiers.addElement(modifier);
	}
	
		
	public Form buildUIForm() {
		final Form f = new Form(model.getFormId());
		
		Enumeration en = model.getFormControls();
		
		final Hashtable t = new Hashtable();
		
		while (en.hasMoreElements()) {
			Item item = null;
			AbstractFormControl ctrl = (AbstractFormControl) en.nextElement();	
			
			if (ctrl instanceof InputFormControl) {
				InputFormControl ifc = (InputFormControl) ctrl;
				switch (ifc.getType()) {
					case InputFormControl.TYPE_TEXT:
					case InputFormControl.TYPE_TEXTAREA:
						int maxLen = ifc.getMaxLength() == null ? 150 : ifc.getMaxLength().intValue();
						item = new TextField(ifc.getName(), ifc.getValue(), maxLen, TextField.ANY);
						t.put(item, ifc);
						break;
					case InputFormControl.TYPE_CHECKBOX:
						// TODO
						break;
				}
			} else if (ctrl instanceof SelectFormControl) {
				SelectFormControl slct = (SelectFormControl) ctrl;
				item = new ChoiceGroup(slct.getName(), ChoiceGroup.POPUP, slct.getOptionTexts(), null);
				t.put(item, slct);
			}
			
			Enumeration modifEn = modifiers.elements();
			while (modifEn.hasMoreElements()) {
				ItemModifier modifier = (ItemModifier) modifEn.nextElement();
				if (item != null)
					item = modifier.modifyItem(item);
				else
					break;
			}
			
			if (item != null)
				f.append(item);
			
			f.setItemStateListener(new ItemStateListener() {
				
				public void itemStateChanged(Item item) {
					// update form model with data from UI
					if (item instanceof TextField) {
						TextField tf = (TextField) item;
						InputFormControl ifc = (InputFormControl) t.get(item);
						ifc.setValue(tf.getString());
						// +++ hack till i find a better solution :)
						if (item.getLabel().equals("Message")){
							// update messlen field
							TextField messLenTf = (TextField) f.get(4);
							messLenTf.setString("" + (150 - tf.getString().length()));
							messLenTf.notifyStateChanged();
						}
						// +++ end hack
						System.out.println("New value for input control "
								+ ifc.getName() + ": " + ifc.getValue());
					} else if (item instanceof ChoiceGroup) {
						ChoiceGroup chg = (ChoiceGroup) item;
						SelectFormControl slct = (SelectFormControl) t.get(item);
						slct.setSelectedIndx(chg.getSelectedIndex());
						System.out.println("New value for select "
								+ slct.getName() + ": "
								+ slct.getSelectedIndx());
					}
				}
			});
		}
		
		return f;
	}
}
